//
//  XhlGuideViewController.m
//  CqlivingCloud
//
//  Created by xhl on 2021/7/21.
//  Copyright © 2021 xinhualong. All rights reserved.
//

#import "XhlGuideViewController.h"
#import "AppDelegate.h"
#import "TabbarViewController.h"

#pragma mark - CQGuideViewCell
@interface XhlGuideViewCell : UICollectionViewCell

@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UIButton *buttonNext;

@end

@implementation XhlGuideViewCell

- (instancetype)init {
    self = [super init];
    if (self) {
        [self configureView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configureView];
    }
    return self;
}

- (void)configureView {
    
    self.backgroundColor = UIColor.whiteColor;
    self.imageView = [[UIImageView alloc] init];
    self.imageView.backgroundColor = UIColor.whiteColor;
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(-30);
    }];
    
    
    self.buttonNext = [[UIButton alloc] init];
    self.buttonNext.backgroundColor = AppThemeColor;
    [self.buttonNext setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    [self.buttonNext setTitle:@"Next" forState:UIControlStateNormal];
    [self.buttonNext.titleLabel setFont: [UIFont systemFontOfSize:18 weight:UIFontWeightMedium]];
    self.buttonNext.cornerRadius = 25;
    [self.contentView addSubview:self.buttonNext];
    [self.buttonNext mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.offset(50);
        make.bottom.mas_equalTo(-15-SYS_TabBarFloatHeight);
    }];

}

@end


@interface XhlGuideViewController ()
<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UIViewController *rootViewController;

@property (nonatomic,strong) NSArray *images;

@property (strong, nonatomic) UICollectionView *collectionView;

@end

@implementation XhlGuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.collectionView];
    
//    __weak __typeof__(self) weakSelf = self;
//    [self.view xhl_addTapActionWithBlock:^(UIGestureRecognizer * _Nonnull gestureRecoginzer) {
//
//
//        //标记引导图已经展示并且用户手动进入首页
//        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
//        NSString *version = [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"];
//
//        NSString *key = [NSString stringWithFormat:@"Guide_%@", version];
//        [ud setBool:YES forKey:key];
//        [ud synchronize];
//
//        __typeof__(self) strongSelf = weakSelf;
//        [XhlGuideManager restoreRootViewController:strongSelf.rootViewController];
//        if (strongSelf.model.guideEndPlays)strongSelf.model.guideEndPlays();
//        if (strongSelf.model.arouseEndPlays)strongSelf.model.arouseEndPlays();
//
//    }];
    
    self.images = @[@"guid1",@"guid2"];
}



#pragma mark - UICollectionViewDelegate/DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    XhlGuideViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseCell" forIndexPath:indexPath];
    
    NSString *path = [self.images objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:path];
    if(indexPath.row == 0){
        [cell.buttonNext setTitle:@"Next" forState:UIControlStateNormal];
    }else{
        [cell.buttonNext setTitle:@"Open" forState:UIControlStateNormal];
        
    }
    cell.buttonNext.tag = indexPath.row +100;
    [cell.buttonNext addTarget:self action:@selector(clickOpen:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (void)clickOpen:(UIButton *)button{
    
    NSInteger row = button.tag - 100;
    if(row == 0){
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    }else{
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setBool:YES forKey:@"hasLaunchedBefore"];
        [defaults synchronize];
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        TabbarViewController *tabbarVC = [[TabbarViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tabbarVC];
        appDelegate.window.rootViewController = nav;
        [appDelegate.window makeKeyAndVisible];
    }
    
    
}

- (CGSize)adapterSizeImageSize:(CGSize)is compareSize:(CGSize)cs {
    CGFloat w = cs.width;
    CGFloat h = cs.width / is.width * is.height;
    
    if (h < cs.height) {
        w = cs.height / h * w;
        h = cs.height;
    }
    return CGSizeMake(w, h);
}
#pragma mark - Lazy
- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        CGRect screen = [UIScreen mainScreen].bounds;
        UICollectionViewFlowLayout *flawLayout = [[UICollectionViewFlowLayout alloc] init];
        flawLayout.minimumInteritemSpacing = 0;
        flawLayout.minimumLineSpacing = 0;
        flawLayout.itemSize = screen.size;
        flawLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:screen collectionViewLayout:flawLayout];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.bounces = NO;
        
        [_collectionView registerClass:[XhlGuideViewCell class] forCellWithReuseIdentifier:@"reuseCell"];
    }
    return _collectionView;
}
@end
